﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OF.Web.user
{
    public partial class address : BuyerAdminPage
    {
        string strwhere = "";
        BLL.Addr.AreaCache bll_cache = new BLL.Addr.AreaCache();
        DAL.BuyerAddress dal_address = new DAL.BuyerAddress();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hfid.Value = "0";
                Bind();
            }
        }

        public void Bind()
        {
            strwhere = " buyerid=" + WebUtils.GetBuyerId();
            repList.DataSource = dal_address.GetList(strwhere);
            repList.DataBind();
        }

        public string GetName(object areaid)
        {
            if (areaid == null) return string.Empty;
            return bll_cache.GetAreaNameById(Convert.ToInt32(areaid));

        }

        public string GetState(object obj)
        {
            if (obj != null)
            {
                string state = obj.ToString();
                if (state == "0")
                    return "停用";
                if (state == "1")
                    return "使用中";
                if (state == "2")
                    return "默认";
            }
            return string.Empty;

        }

        public string GetSchoolName(object schoolid)
        {
            if (schoolid == null) return string.Empty;
            var model = bll_cache.GetSchoolById(Convert.ToInt32(schoolid));
            if (model == null) return string.Empty;
            return model.shcoolname;

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string province = hfProvince.Value;
            string city = hfCity.Value;
            string district = hfDistrict.Value;
            string school = hfSchool.Value;

            Model.BuyerAddress model = new Model.BuyerAddress();
            model.province = string.IsNullOrWhiteSpace(province) ? 0 : Convert.ToInt32(province);
            model.city = string.IsNullOrWhiteSpace(city) ? 0 : Convert.ToInt32(city);
            model.area = string.IsNullOrWhiteSpace(district) ? 0 : Convert.ToInt32(district);
            model.school = string.IsNullOrWhiteSpace(school) ? 0 : Convert.ToInt32(school);
            model.address = txtaddress.Text;
            model.state = Convert.ToInt32(ddlState.SelectedValue);
            model.buyerid = WebUtils.GetBuyerId();
            bool result = false;
            //编辑
            if (hfid.Value != "0")
            {
                model.id = Convert.ToInt32(hfid.Value);
                //设为默认
                if (model.state == 2) {
                    //取消旧的默认记录
                    dal_address.UpdateState(model.id, 2,1);
                }
                result = dal_address.Update(model);
            }
            else
            {
                //验证是否超过限制
                if(dal_address.GetRecordCount(" buyerid="+model.buyerid)>=5){
                    Common.MessageBox.Show(this, "最多只能保存5个地址");
                    return;
                }
                result = dal_address.Add(model) > 0 ? true : false;
            }

            if (result)
            {
                Common.MessageBox.Show(this, "保存成功");
                Bind();
            }
            else
            {
                Common.MessageBox.Show(this, "保存失败,请稍后重试");
            }
        }

        protected void repList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (dal_address.Delete(id))
            {
                Common.MessageBox.Show(this, "删除成功");
                Bind();
            }
            else {
                Common.MessageBox.Show(this, "删除失败");
            }
        }
    }
}